# import tesserocr
import base64
import random
from eagleocr.split import imgDispose
from eagleocr.recg import tesser
from io import BytesIO
from PIL import Image
from fe_api.imgrecog.utils import rds


def base64_to_img(base64img):
    return imgDispose.rgba_get_2v_img(
        Image.open(BytesIO(base64.b64decode(base64img))))


def get_word_txt(img, lang):
    # txt = tesser.get_word_txt(img, 'xj2', 6)
    txt = tesser.get_word_txt(img, lang, 6)
    if txt:
        return txt
    return ''


def judge_word_txt(r_txt, img, hw_stroke, user_id=None):
    # word_stroke = rds.WordStroke()
    # if int(hw_stroke) < word_stroke.get(r_txt):
    #     return False, ''
    # if user_id is not None:
    #     f = rds.WordHandWriteF().add_f(user_id + r_txt)
    #     if f >= random.randint(3, 6):
    #         return True, r_txt
    img.save('b.png')
    for i in range(1, 16):
        lang = 'sx{0}'.format(i)
        txt = get_word_txt(img, lang)
        print(txt)
        if r_txt in txt:
            # word_stroke.set(r_txt, hw_stroke)
            return True, r_txt
    return False, txt[0]
